<?php
// +----------------------------------------------------------------------
// | Description: 岗位
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\common\model;

use app\common\model\base\BaseModel;
use app\common\traits\InstanceTrait;
use think\exception\DbException;
use think\model\Collection;

class AdminPost extends BaseModel
{

    use InstanceTrait;

    public function __construct($data = [], Model $parent = null, $table = '')
    {
        parent::__construct($data, $parent, $table);
        $this->pk = 'post_id';
        $this->initBaseField();
    }

    /**
     * 获取列表
     * @return Collection
     * @throws DbException
     */
    public function getList($param)
    {
        $where = $this->getBaseWhere($param);
        $param['page'] = !empty($param['page']) ? $param['page'] : 1;
        $param['limit'] = !empty($param['limit']) ? $param['limit'] : 15;
        $data = static::where($where)
                      ->field($this->baseField)
                      ->page($param['page'], $param['limit'])
                      ->select();
        return $data;
    }

    /**
     * 获取where
     * @param array $param
     * @return array|mixed
     */
    public function getBaseWhere($param = [], $alias = '')
    {
        $where = parent::getBaseWhere($param, $alias);
        if (!empty($param['structure_id'])) {
            $where[] = ['structure_id', '=', $param['structure_id']];
        }
        return $where;
    }

    /**
     * 关联多对一-部门
     * @return \think\model\relation\HasMany
     */
    public function structure()
    {
        return $this->belongsTo(AdminStructure::class, 'structure_id', 'structure_id')
                    ->where('status', '=', 1)
                    ->alias('structure')
                    ->field(['structure.*', 'structure.structure_id as id']);
    }

    public function admins()
    {
        return $this->hasMany(Admin::class, 'post_id', 'post_id')
                    ->alias('admin')
                    ->field(Admin::instance()->baseField)
                    ->where('status', '=', 1);
    }
}
